test

TypeScript and Why You Should Use It

29 April, 2019 | 4 min read

What is TypeScript?

TypeScript brings you optional static type-checking along with the latest ECMAScript features. — TypeScript website

If you don’t know what TypeScript is, I suggest that you read this great post by Charly Poly.

If you still don’t understand/know what it is, TypeScript introduces type checking to your JavaScript. What does that actually mean?!?! Well, allow me to explain 😃.

In standard JavaScript, you can set a variable to a string on one line and to a boolean on the next 😦. Some people say this is what makes JavaScript so powerful, but I disagree (to a certain extent, put ya pitchforks down!).

let test = "hello world"
    test = false; //this works but is bad 😕
copied

This is what makes JavaScript a loosely/weakly typed language.

But why is this bad?!? — literally you

Well, once you start working with larger teams with a large codebase you’ll really start to see the issue. Some of your teammates may misuse a particular method/API, or they may inadvertently change the type of a variable which could lead to a 💩 ton of bugs, among many other things!

So TypeScript comes to the rescue, by introducing types and type checking. You can, for example, set the type for a variable, and if you try to change its type the TypeScript compiler will through an error, letting you know what you did wrong:

let test: string = "hello world"; test = false; //Type 'false' is not assignable to type 'string'.

TL;DR: Seriously! Just go back up and read the section!


TypeScript is great and all (all hail Microsoft), but it only does type checking at compile time.

Obviously, your browser doesn’t understand TypeScript and because of that TypeScript is compiled down to JavaScript. Which is when TypeScript will do the type checking for ya. But with that, you lose type checking at runtime. So when your application is running (runtime) types can be changed.

Jenkins Nodejs

Not an alternative to Tests! 🧪

For the love of god adding types to your JavaScript is not an alternative to writing tests! That's all I’m going to say.

Good for Large Teams

Like I mentioned in the what is TypeScript section, it’s great for large teams. Let me give you an example.

Let's say that you are working on a project with 10 fellow developers 💻, and you are working on the login function(s) which would be used throughout the project, while your best bud, Bob, is developing the cart feature.

Typically Bob would have to either read the documentation (if there even is any, which let's be honest, there isn’t) for the login function(s) that you wrote, or he would have to find and understand your code. Even then, he could misuse the login API by passing the wrong values or whatever. Now multiply that by 10 developers and you start to see how there could be a 💩 ton of bugs!

However, if your project was written in TypeScript, and Bob utilized your login function(s) improperly, the compiler would tell him how he misused it, or if Bob was cool and was using VSCode, then it would just pop up in his IDE.

Sharing Type Declarations

If you have a full stack JavaScript application then you’re in luck! You can share type declarations between the frontend and backend. What does that mean?

Let's say that you have a backend endpoint, which accepts x params and returns x object. You can have a type declaration for whatever method that is called when you hit that endpoint, which defines the signature and return type. This declaration can then be used by whatever method that calls the endpoint on the frontend!

Why? Well, it reduces the amount of duplicated type declarations in your codebase and allows you to define everything for an API in one place so that you only have to change the declaration in one file instead of multiple.

Please Don’t Use the Any Type

If you do decide to use TypeScript, it can be tempting to just use the any type everywhere, but please don’t! It defeats the purpose of TypeScript entirely, only use it as a last resort!

Conclusion

Hopefully, I’ve convinced you to use TypeScript, or at the very least to try it out. Obviously, there is so much more to TypeScript and I’m sure people will argue for and against it until the end of time, but ultimately it’s your choice.